package com.xudu.controller.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xudu.controller.IUserController;
import com.xudu.entity.User;
import com.xudu.service.IUserService;
import com.xudu.utils.Jwt;
import com.xudu.utils.Resoult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * ClassName: UserControllerImpl
 * Description:
 *
 * @Author xudu
 * @Create 2024/4/9
 */
@RestController
@ResponseBody
@RequestMapping("user")
public class UserControllerImpl implements IUserController {

    /**
     * 调用业务层
     */
    @Autowired
    private IUserService iUserService;


    @Override
    @GetMapping("/login")
    public Resoult login(String username, String password) {


        return iUserService.login(username, password);



    }


    /**
     * 测试拦截器接口
     * @return
     */
    @Override
    @GetMapping("test")
    public Resoult test() {

        System.out.println("111111");
        Resoult resoult = new Resoult();
        return resoult.success();


    }

    /**
     * 通过token获取用户信息接口
     * @param token
     * @return
     */
    //@RequestHeader("Authorization") String
    @Override
    @GetMapping("getInfo")
    public Resoult getInfo(@RequestHeader String token) {
        //String jwt = token.split(" ")[1];
        Map<String, Object> map = Jwt.getPayload(token);
        //return Resoult.success(map.get("username"), token);
        return null;
    }



    /**
     * 注册用户
     *
     * @param user
     */
    @Override
    @GetMapping("register")
    public void register(@RequestBody User user) {
        iUserService.register(user);
    }

    /**
     * 添加用户并上传照片 将照片存储路径保存在数据库中
     *
     * @param username
     * @param password
     * @param photo
     * @return
     * @throws IOException
     */
    @Override
    @PostMapping("registerWithPhoto")
    public Resoult uploadPhoto(String username, String password, MultipartFile photo) {

        return iUserService.saveUserWithPhoto(username, password, photo);
    }


    /**
     * 通过用户名获取用户照片
     * @param username
     * @param response
     * @throws IOException
     */

    @Override
    @GetMapping("getphoto")
    public void getphoto(String username, HttpServletResponse response) throws IOException {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = iUserService.getOne(queryWrapper);

        File photoFile = new File(user.getImgPath());
        byte[] bytes = FileUtil.readBytes(photoFile); //FileUtil是hutool工具封装的工具类
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }

}
